<?php
class Excel extends Controller {
	function __construct(){
        parent::__construct();
        $this->load->model("models");
		$this->load->model("version");
		$this->load->model("complete");
	    $this->load->model("category");
	    $this->load->model("company");
	    $this->load->model("receive");
	    $this->load->model("send");
	    $this->load->model("update_historys");
	    $this->load->model("inventory");
	    $this->load->model("historys");
	    $this->load->model("codes");
	    $this->load->model("import_temp");
	    $this->load->helper('file');
	    $this->load->library('zip');
	    $this->load->model("second_send");
	}  

	
	function import_simple(){
		if($_POST){
			error_reporting(E_ALL ^ E_NOTICE);
			require_once APPPATH.'libraries/excelreader.class.php';
			$path = "uploads/";
			$name = $_FILES['excel_import']['name'];
			$path=$path.$name;
			move_uploaded_file($_FILES["excel_import"]["tmp_name"],$path);
			if(is_readable($path)){
				$data = new Spreadsheet_Excel_Reader();
				$data->setOutputEncoding('UTF-8');
				$data->read($path);
				$filmarray = $data;
			}else{
				return "文件不可读";
			}
				
			$sheet_names = array();
			foreach($data->boundsheets as $k=>$val){
				$sheet_names[]=$val['name'];
			}
			
			$vals = array();
			$lev = array('机密'=>0,'秘密'=>1,'内部'=>2);			 
			foreach($data->sheets as $k=>$val){
	
				if($k>0){
					$vals=$val['cells'];
					foreach ($vals as $j=>$l){
						if($j>1){
						$codes_array = array();
						$lev = array('机密'=>0,'秘密'=>1,'内部'=>2);


						$mod_name        =  trim($l[2]);							
						$category_name   = trim($l[4]);
						$level           =  $lev[$l[5]];
						$codes           = trim($l[3]);
						$receive_code    =  trim($l[6]);
						$receivetime     = trim($l[7]);
						$receive_count   = trim($l[8]);
						$send_com        = trim($l[9]);
						$send_time       = trim($l[10]);
						$send_code       = trim($l[11]);
						$send_count      = trim($l[12]);
						$first_version   = trim($l[13]);
						$update_code     = trim($l[14]);
						$update_time     = trim($l[15]);
						$update_count    = trim($l[16]);
						$current_version = trim($l[17]);
						$affect_version  = trim($l[18]);
					//	echo '<pre>';
						//insert models
						if($mod_name!=''&&$mod_name!='empty'){
							$mod_array = array('name'=>$mod_name);
							$mod_id = $this->models->insert_for_excel($mod_array);
						}
						//insert categorys
						if($category_name!=''&&$category_name!='empty'){
							$u_time='';
							$category_array = array('name'=>$category_name,'mod_id'=>$mod_id,'codes'=>$codes,'level'=>$level,'updatetime'=>$u_time);
							$category_id = $this->category->insert_for_excel($category_array);
						}
						
					//	echo $current_version;
						//insert versions
						if($first_version!=''&&$first_version!='empty'){
							$version_array = array('name'=>$first_version,'mod_id'=>$mod_id,'category_id'=>$category_id);
							$first_version_id = $this->version->insert_for_excel($version_array);
						}
						if($current_version!=''&&$current_version!='empty'){
							$current_version_array = array('name'=>$current_version,'mod_id'=>$mod_id,'category_id'=>$category_id);
							$receive_version_id = $this->version->insert_for_excel($current_version_array);
						}
						if($affect_version!=''&&$affect_version!='empty'){
							$affect_version_array = array('name'=>$affect_version,'mod_id'=>$mod_id,'category_id'=>$category_id);
							$affect_version_id = $this->version->insert_for_excel($affect_version_array);
						}

						
						//calculator  receive count
						 if($receive_code!=''){  
	                     $receive_array = array('mod_id'=>$mod_id,'current_version'=>$current_version,'first_version'=>$first_version,'first_version_id'=>$first_version_id,'category_id'=>$category_id,'version_id'=>$receive_version_id,'codes'=>$receive_code,'receivetime'=>$receivetime,'count'=>$receive_count);
	                     $receive_id = $this->receive->insert_for_excel($receive_array);
	                      $ar = array('mod_id'=>$mod_id,
	                                  'user'=>'',
	                                  'category_id'=>$category_id,
	                                  'version_id'=>$receive_version_id,
	                                  'count'=>$receive_count,
	                      		      'receivetime'=>$receivetime,
	                                  'r_codes'=>$receive_code,
	                      		      's_codes'=>$send_code,
	                                  'createtime'=>time(),
	                                  's_r_id'=>$receive_id,
	                                  'mod'=>$mod_name,
	                                  'category'=>$category_name,
	                                  'version'=>$current_version,
	                                  'type'=>2,
	                      			  'is_update'=>0
	                      );
						$this->historys->insert($ar);
	                   }   
	             //calculator send count 	                  
	                   if((trim($send_com)!='601'&&$send_code!=''&&$send_count!=''&&$send_count!='0')&&(intval($send_count)>0)){
	                   	//select_send_company
	                     $company_array = array('name'=>$send_com); 
	                   
	                     $company_id = $this->company->insert_for_excel($company_array);      
	                     $send_array = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$receive_version_id,'codes'=>$send_code,'sendtime'=>$send_time,'company_id'=>$company_id,'count'=>$send_count);
	                     $send_id = $this->send->insert_for_excel($send_array);
		                 $ar = array('mod_id'=>$mod_id,
		                                  'user'=>'',
		                                  'category_id'=>$category_id,
		                                  'version_id'=>$receive_version_id,
		                                  'count'=>$send_count,
		                     		      'sendtime'=>$send_time,
		                                  'r_codes'=>$receive_code,
		                     		      's_codes'=>$send_code,
		                                  'createtime'=>time(),
		                                  's_r_id'=>$send_id,
		                                  'company_id'=>$company_id,
		                                  'mod'=>$mod_name,
		                                  'category'=>$category_name,
		                                  'version'=>$current_version,
		                                  'type'=>1
		                      );
							$this->historys->insert($ar);
						
	                   }
	              //inventory
	                    if(trim($send_com)=='601'){
	                       $inven_array = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$receive_version_id,'count'=>$update_count);
	                       $this->inventory->insert_for_excel($inven_array);
	                     }     
			    //二次发放
			            if(intval($send_count)<0){
			            	$company_array = array('name'=>$send_com);
			            	$company_id = $this->company->insert_for_excel($company_array);
			            	$second_data = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$current_version,'company_id'=>$company_id,'count'=>$send_count,'sendtime'=>$send_time,'codes'=>$send_code,'createtime'=>time());
                            $this->second_send->insert($second_data);
			            }                
			             //Update datas
			                  if($update_code!=''&&$update_code!='empty'){
			                  	if($send_com!='601'){
				                  	$company_array = array('name'=>$send_com);			                  	
				                  	$company_id = $this->company->insert_for_excel($company_array);
			                  	}
			                  	$array = array(
			                  			'mod_id'=>$mod_id,
			                  			'category_id'=>$category_id,
			                  			'codes'=>trim($update_code),
			                  			'version_id'=>$receive_version_id,
			                  			'pre_version'=>$first_version_id,
			                  			'count'=>$update_count,
			                  			'updatetime'=>$update_time,
			                  			'createtime'=>time(),
			                  			'company_id'=>$company_id,	                  	
			                  	);
			                  	$this->update_historys->insert_for_excel($array);
			                  } 
						}
					}
				}
			}
		
		//	echo "<script>alert('导入成功！')</script>";
	   redirect('main/index');
		}else{
				
			$this->top();
			$this->load->view('import_simple');
		
		}
	}
	
	
	function truncate(){
		$this->category->truncate();
		$this->company->truncate();
		$this->complete->truncate();
		$this->historys->truncate();
		$this->import_temp->truncate();
		$this->codes->truncate();
		$this->inventory->truncate();
		$this->models->truncate();
		$this->second_send->truncate();
		$this->receive->truncate();
		$this->update_historys->truncate();
		$this->send->truncate();
		$this->version->truncate();
		redirect('main/index');
	}
	
	public function export(){
		
		    //Get all datas:model,category,version,receive,send
				$models    = $this->models->select('');
				
	          
          $file_name = array();
	    //First sheet end and Other sheets started
	       foreach ($models as $key=>$val){
				$receive_data = $this->receive->select_by_array(array('mod_id'=>$val->mod_id));		
				$send_data = $this->send->sel2(array('mod_id'=>$val->mod_id));
				$update_data = $this->update_historys->select_by_array(array('mod_id'=>$val->mod_id));
				$second_send_data = $this->second_send->select_by_array(array('mod_id'=>$val->mod_id));
				$total_datas = array_merge($receive_data,$send_data);
		
				$total_datas = array_merge($total_datas,$update_data);
				$total_datas = array_merge($total_datas,$second_send_data);
			   $name = $this->generate_excel($total_datas,$models,$val->name);
			   $file_name[]=$name;
			   $this->zip->read_file($name);			
	       }

	       $this->zip->download('back_up.zip');
		  //redirect($name);  	
	}
	
	public function generate_excel($send_data,$models,$mod_name){
		$categorys = $this->category->select('');
		$lev = array('0'=>'机密','1'=>'秘密','2'=>'内部');
		require_once APPPATH.'libraries/PHPExcel/PHPExcel.php';
		require_once APPPATH.'libraries/PHPExcel/PHPExcel/IOFactory.php';
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
		->setLastModifiedBy("Maarten Balliauw")
		->setTitle("Office 2007 XLSX Test Document")
		->setSubject("Office 2007 XLSX Test Document")
		->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
		->setKeywords("office 2007 openxml php")
		->setCategory("Test result file");
		 
		//First sheet started
		$objPHPExcel->setActiveSheetIndex(0);
		$objRichText = new PHPExcel_RichText();
		$objRichText->createText('');
		$objPayable = $objRichText->createTextRun('PHP导出的Excel');
		
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(200);
		
		$objPHPExcel->getActiveSheet()->setCellValue('A1', '编码');
		$objPHPExcel->getActiveSheet()->setCellValue('B1', '型号');
		$objPHPExcel->getActiveSheet()->setCellValue('C1', '书名');
		 
		$i=2;
		foreach($categorys as $k=>$val){
			foreach ($models as $j=>$m){
				if($val->mod_id == $m->mod_id){
					$mod = $m->name;
				}
			}
			$objPHPExcel->getActiveSheet()->setCellValueExplicit('A'.$i, $val->codes,PHPExcel_Cell_DataType::TYPE_STRING);
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $mod);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $val->name);
			$i++;
		}
		$objPHPExcel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('编码对照表');
// 		foreach ($send_data as $key=>$val){
// 			$datas[$val->codes][] = $val;
// 		}
			
		 
		$objPHPExcel->createSheet();
		$objPHPExcel->getSheet(1)->setTitle('资料备份');
		$objPHPExcel->setActiveSheetIndex(1);
		$objRichText = new PHPExcel_RichText();
		$objRichText->createText('');
		
		$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(18);
		$objPHPExcel->getActiveSheet()->setCellValue('A1', '编号(份数)');
		$objPHPExcel->getActiveSheet()->setCellValue('B1', '型代号');
		$objPHPExcel->getActiveSheet()->setCellValue('C1', '编码');
		$objPHPExcel->getActiveSheet()->setCellValue('D1', '正题名');
		$objPHPExcel->getActiveSheet()->setCellValue('E1', '密级');
		$objPHPExcel->getActiveSheet()->setCellValue('F1', '接收单号');
		$objPHPExcel->getActiveSheet()->setCellValue('G1', '接收日期');
		$objPHPExcel->getActiveSheet()->setCellValue('H1', '接收份数');
		$objPHPExcel->getActiveSheet()->setCellValue('I1', '发往单位(份数)');
		$objPHPExcel->getActiveSheet()->setCellValue('J1', '发出日期');
		$objPHPExcel->getActiveSheet()->setCellValue('K1', '发放依据(单号)');
		$objPHPExcel->getActiveSheet()->setCellValue('L1', '发放份数');
		$objPHPExcel->getActiveSheet()->setCellValue('M1', '初始版次(有效日期)');
		$objPHPExcel->getActiveSheet()->setCellValue('N1', '更改依据(单号)');
		$objPHPExcel->getActiveSheet()->setCellValue('O1', '更改时间');
		$objPHPExcel->getActiveSheet()->setCellValue('P1', '更新份数');
		$objPHPExcel->getActiveSheet()->setCellValue('Q1', '当前版次(有效日期)');
		$objPHPExcel->getActiveSheet()->setCellValue('R1', '有效版次(有效日期)');
		$objPHPExcel->getActiveSheet()->setCellValue('S1', '更改附注');
		$objPHPExcel->getActiveSheet()->setCellValue('T1', '附注');
		
		foreach($send_data as $j=>$m){
			if(isset($m->company_id)){
				$company = $this->company->select($m->company_id);
				if(count($company)==1){
					$com = $company[0]->name;
				}else{
					$com = '';
				}
			}else{
				$com = '';
			}
		
			$newest_version = $this->version->get_new_version(array('category_id'=>$m->category_id));
			$cu_version = $newest_version[0]->name;
			$lastest_version = $this->version->get_last_version(array('category_id'=>$m->category_id));
			$category_c = $this->category->select(array('category_id'=>$m->category_id));
			$mod_c = $this->models->select($m->mod_id);
			if(!isset($m->s_codes)){
				$m->s_codes='';
			}
			if(isset($m->sendtime)){
				$c_v = $this->version->select(array('version_id'=>$m->version_id));
				$m->version = $c_v[0]->name;
			}
			
			if(isset($m->pre_version)){
				$update_codes=$m->codes;
				$update_time=$m->updatetime;
				$update_count=$m->count;
				$now_v = $this->version->select(array('version_id'=>$m->version_id));
				$m->version = $now_v[0]->name;
				$now_v2 = $this->version->select(array('version_id'=>$m->pre_version));
				$cu_version = $now_v2[0]->name;
				if($m->company_id==0){
					$com='601';
				}
				$m->count='';

			}else{
				$update_time = "";
				$update_codes="";
				$update_count="";
				//$m->version="";
			}
			if(isset($m->receivetime)){
				$m->r_codes = $m->codes;
				$cu_version= $m->first_version;
				$m->version = $m->current_version;
			}else{
				$m->r_codes = "";
			}
			
			if(intval($m->count)<0){
				$cu_version='';
			}
			$l = $this->category->select(array('category_id'=>$m->category_id));
			$a_sheet = "A".($j+2);
			$b_sheet = "B".($j+2);
			$c_sheet = "C".($j+2);
			$d_sheet = "D".($j+2);
			$e_sheet = "E".($j+2);
			$f_sheet = "F".($j+2);
			$g_sheet = "G".($j+2);
			$h_sheet = "H".($j+2);
			$i_sheet = "I".($j+2);
			$j_sheet = "J".($j+2);
			$k_sheet = "K".($j+2);
			$l_sheet = "L".($j+2);
			$m_sheet = "M".($j+2);
			$n_sheet = "N".($j+2);
			$o_sheet = "O".($j+2);
			$p_sheet = "P".($j+2);
			$q_sheet = "Q".($j+2);
			$r_sheet = "R".($j+2);
			$s_sheet = "S".($j+2);
			$t_sheet = "T".($j+2);
			$objPHPExcel->getActiveSheet()->setCellValue($a_sheet,$j+1);
			$objPHPExcel->getActiveSheet()->setCellValue($b_sheet,isset($m->mod)?$m->mod:$mod_c[0]->name);
			$objPHPExcel->getActiveSheet()->setCellValueExplicit($c_sheet,$category_c[0]->codes,PHPExcel_Cell_DataType::TYPE_STRING);
			$objPHPExcel->getActiveSheet()->setCellValue($d_sheet,isset($m->category)?$m->category:$category_c[0]->name);
			$objPHPExcel->getActiveSheet()->setCellValue($e_sheet,$lev[$l[0]->level]);
			$objPHPExcel->getActiveSheet()->setCellValue($f_sheet,isset($m->r_codes)?$m->r_codes:'');
			$objPHPExcel->getActiveSheet()->setCellValue($g_sheet,isset($m->receivetime)?$m->receivetime:'');
			$objPHPExcel->getActiveSheet()->setCellValue($h_sheet,empty($com)?$m->count:'');
			$objPHPExcel->getActiveSheet()->setCellValue($i_sheet,$com);
			$objPHPExcel->getActiveSheet()->setCellValue($j_sheet,isset($m->sendtime)?$m->sendtime:'');
			$objPHPExcel->getActiveSheet()->setCellValue($k_sheet,!empty($com)?$m->codes:'');
			$objPHPExcel->getActiveSheet()->setCellValue($l_sheet,!empty($com)?$m->count:'');
			$objPHPExcel->getActiveSheet()->setCellValue($m_sheet,$cu_version);
			$objPHPExcel->getActiveSheet()->setCellValue($n_sheet,$update_codes);
			$objPHPExcel->getActiveSheet()->setCellValue($o_sheet,$update_time);
			$objPHPExcel->getActiveSheet()->setCellValue($p_sheet,$update_count);
			$objPHPExcel->getActiveSheet()->setCellValue($q_sheet,isset($m->version)?$m->version:'');
			$objPHPExcel->getActiveSheet()->setCellValue($r_sheet,$lastest_version[0]->name);
			$objPHPExcel->getActiveSheet()->setCellValue($s_sheet,'');
			$objPHPExcel->getActiveSheet()->setCellValue($t_sheet,'');
		
			$objPHPExcel->setActiveSheetIndex(1);
		
		}
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		date_default_timezone_set('Asia/Chongqing');
		$name = 'img/excel/export_'.$mod_name.date('Y.m.d h.i.s').'.xls';
		$name = iconv('utf-8','gb2312',$name);
		$objWriter->save($name);
		return $name;
	}

	public function import(){
		// 先清空临时表
		$this->import_temp->truncate();
		if($_POST){
			//error_reporting(E_ALL ^ E_NOTICE);
		    require_once APPPATH.'libraries/Excel/reader.php';
		    $path = "uploads/";
			$name = $_FILES['excel_import']['name'];
			$path=$path.$name;
			move_uploaded_file($_FILES["excel_import"]["tmp_name"],$path);
			if(is_readable($path)){
				$data = new Spreadsheet_Excel_Reader();
				// Set output Encoding.
			//	$data->setOutputEncoding('utf-8');
				$data->read($path);
			}else{
				
				return "文件不可读";
			}
            $sheet_names = array(); 
            foreach($data->boundsheets as $k=>$val){
               $sheet_names[]=$val['name'];
            }

            $vals = array();
      
            foreach ($data->sheets as $k=>$val){
              if($k==0){         
              	  $a = array();
              	 foreach ($val['cells'] as $key=>$v){
              	     $a['name'] = $v[3];
              	     $a['codes'] = $v[1];
              	     $this->send->insert_code($a);
              	     unset($a);
              	 }
             
              }else{	
              	$path = file_path();   	
			    $file = $path.$k.'.txt';
			    $fh = fopen($file, 'w+') or die("folder permission is not right!");	      	
	                foreach ($val['cells'] as $j=>$m){
	                	if($j>1){
		                   $str='';               
		                   for($i=1;$i<16;$i++){
		                   	  if(!isset($m[$i])){ $m[$i]='empty';};
		                      $str.= trim($m[$i]).'&&';		                               
		                   }	
		                   $str = $str.'rn';	                  	           
			                fwrite($fh, $str);          		                    
		                   $str ='';                  
	                  }	         
	                }
              }  
            }   
           redirect('excel/read');
		}else{
			
			$this->top();
			$this->load->view('excel_import');
		
		}
	}
	
	function da(){
	   $codes = $this->send->select_codes();
	   foreach ($codes as $key=>$val){
	     $this->category->update_name($val->name,array('codes'=>$val->codes));
	   }
	  // echo "<script>alert('导入成功！')</script>";
	   redirect('main/index');
	}
	
  function directory($dir){	/* 声明函数 */
		$dp=opendir($dir);	/* 打开目录 */
        $array = array();
		while($file=readdir($dp)){	/* 读取目录 */
			if($file !="." && $file !=".."){	/* 判断是否有"."或者".."文件 */
				$path=$dir. "/". $file;	/* 取得目录路径 */
				if(is_dir($path)){	/* 判断是否有子目录 */
					directory($path);	/* 函数递归调用 */
				}else {
					if(!in_array($file, $array)){
					array_push($array, $file);
					$str=file_get_contents($path);
	                $da = explode('rn', $str);
	                foreach($da as $k=>$val){
	                 if($val!=null){	
	                  $data = explode('&&', $val);	
		              $ar = array();
		              
					    for($i=1;$i<15;$i++){
					    	$ar['temp_'.($i+1)]=$data[$i];
					    	//$ar['temp_1'] = '';
					    }
					    
					   $re = $this->import_temp->insert($ar);
					   if(!$re){
					      print_r($val);
					   }		
	                 }
	                }
					 if($re){
					      unlink($path);
					     // return true;
				     }	
					}else{
						directory($path);	/* 函数递归调用 */
					}   		       		    
				} 
			}
		}
		closedir($dp);
	}
	
	function read(){
     $i = $this->uri->segment(3);
	   $this->directory(file_path());
       redirect('excel/into_database');    
	}
	
	
function m_card_txtread($filename)  
	{
		if (file_exists($filename))
		{ 			
			$file_content_a=file_get_contents($filename);
			return $file_content_a;	
		}else
		{
			return false;
		}
	}
	
	public function into_database(){
	    $data = $this->import_temp->select();
	    foreach ($data as $key=>$val){
	        $this->imp($val);
	    }
	   redirect('excel/da');
	}
	
	
	public function imp($v){
		              $codes_array = array();
		              $lev = array('机密'=>0,'秘密'=>1,'内部'=>2);
	               
	                   $mod_name        = $v->temp_2;					
	                   $category_name   = $v->temp_3;
	                   $codes           = $v->temp_4;
	                   $level           = $lev[$v->temp_5];	                   	                   
	                   $receive_code    = $v->temp_6;                   
	                   $receivetime     = $v->temp_7;
	                   $send_com        = $v->temp_8;
	                   $sendtime        = $v->temp_9;
	                   $send_code       = $v->temp_10;
	                   $first_version   = $v->temp_11;
	                   $update_codes    = $v->temp_12;
	                   $update_version  = $v->temp_13;
	                   $current_version = $v->temp_14;              
	                   $affect_version  = $v->temp_15;
	                   $count           = $v->count;
	                
	             //insert models      
	                   if($mod_name!=''&&$mod_name!='empty'){
	                   	 $mod_array = array('name'=>$mod_name);
	                     $mod_id = $this->models->insert_for_excel($mod_array);
	                   }
	             //insert categorys      
	                   if($category_name!=''&&$category_name!='empty'){
	                   	 if($update_version!='empty'){
	                   	 	$u = explode(';', $update_version);
	                   	 	if(count($u)>0){
	                   	 		$u_time = $u[count($u)-1];
	                   	 	}else{
	                   	 		$u_time =$update_version;
	                   	 	}
	                   	 }else{
	                   	 	$u_time='';
	                   	 }
	                   	 $category_array = array('name'=>$category_name,'mod_id'=>$mod_id,'codes'=>$codes,'level'=>$level,'updatetime'=>$u_time);
	                     $category_id = $this->category->insert_for_excel($category_array);
	                   }
	             //insert versions       
	                   if($first_version!=''&&$first_version!='empty'){
	                     $version_array = array('name'=>$first_version,'mod_id'=>$mod_id,'category_id'=>$category_id);
	                     $first_version_id = $this->version->insert_for_excel($version_array);
	                   }
	                   if($current_version!=''&&$current_version!='empty'){
	                     $current_version_array = array('name'=>$current_version,'mod_id'=>$mod_id,'category_id'=>$category_id);
	                     $receive_version_id = $this->version->insert_for_excel($current_version_array);
	                   }
	                   if($affect_version!=''&&$affect_version!='empty'){
	                     $affect_version_array = array('name'=>$affect_version,'mod_id'=>$mod_id,'category_id'=>$category_id); 
	                     $affect_version_id = $this->version->insert_for_excel($affect_version_array);
	                   }
   
	           
	             //calculator  receive count
	                   if($receive_code!=''){  
	                     $receive_array = array('mod_id'=>$mod_id,'current_version'=>$current_version,'first_version'=>$first_version,'first_version_id'=>$first_version_id,'category_id'=>$category_id,'version_id'=>$first_version_id,'codes'=>$receive_code,'receivetime'=>$receivetime,'count'=>$count);
	                     $receive_id = $this->receive->insert_for_excel($receive_array);
	                      $ar = array('mod_id'=>$mod_id,
	                                  'user'=>'',
	                                  'category_id'=>$category_id,
	                                  'version_id'=>$first_version_id,
	                                  'count'=>$count,
	                      		      'receivetime'=>$receivetime,
	                                  'r_codes'=>$receive_code,
	                                  'createtime'=>time(),
	                                  's_r_id'=>$receive_id,
	                                  'mod'=>$mod_name,
	                                  'category'=>$category_name,
	                                  'version'=>$current_version,
	                                  'type'=>'2'
	                      );
						$this->historys->insert_for_excel($ar);
	                   }   
	             //calculator send count 	                  
	                   if($send_com!='601'&&$send_code!=''){
	        //没有更新记录，三个版次一样，
 	                        if($update_codes==''){
 	                        	$v_version_id = $receive_version_id;
 	                        	$v_version = $current_version;
 	                        }
// 	        //更新时间小于发放时间，是发放前更新，版次取当前版次
// 	        //更新时间大于发放时间，是发放后更新，版次取初始版次
	                        if($update_version<=$sendtime){
	                        	$v_version_id = $receive_version_id;
	                        	$v_version = $current_version;
	                        }else{
	                        	$v_version_id = $first_version_id;
	                        	$v_version = $first_version;
	                        }
	                   	
	                   	//select_send_company
	                     $company_array = array('name'=>$send_com); 
	                     $company_id = $this->company->insert_for_excel($company_array);      
	                     $send_array = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$receive_version_id,'codes'=>$send_code,'sendtime'=>$sendtime,'company_id'=>$company_id,'count'=>$count);
	                     $send_id = $this->send->insert_for_excel($send_array);
		                 $ar = array('mod_id'=>$mod_id,
		                                  'user'=>'',
		                                  'category_id'=>$category_id,
		                                  'version_id'=>$v_version_id,
		                                  'count'=>$count,
		                     		      'sendtime'=>$sendtime,
		                     		      's_codes'=>$send_code,
		                                  'createtime'=>time(),
		                                  's_r_id'=>$send_id,
		                                  'company_id'=>$company_id,
		                                  'mod'=>$mod_name,
		                                  'category'=>$category_name,
		                                  'version'=>$v_version,
		                                  'type'=>'1'
		                      );
							$this->historys->insert_for_excel($ar);
						
	                   }else{
	                     	$company_id="";
	                   }  
	              //inventory
	                    if($send_com=='601'){
	                       $inven_array = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$receive_version_id,'count'=>$count);
	                       $this->inventory->insert_for_excel($inven_array);
	                     }     
	                    
	             //Update datas
	                  if($update_codes!=''&&$update_codes!='empty'){
	                  	$array = array(
	                  			'mod_id'=>$mod_id,
	                  			'category_id'=>$category_id,
	                  			'codes'=>trim($update_codes),
	                  			'version_id'=>$receive_version_id,
	                  			'pre_version'=>$first_version_id,
	                  			'count'=>$count,
	                  			'updatetime'=>$update_version,
	                  			'createtime'=>time(),
	                  			'company_id'=>$company_id,	                  	
	                  	);
	                  	$this->update_historys->insert_for_excel($array);
	                  }      	                   
	                 
	}
	
	public function top(){
			$user = $this->session->userdata('user');
	
			if($user!=""){
				$data['user'] = $user;
			}else{ 
				//直接跳转到登录页面
				redirect('main/login');exit;
			}
		    $data['module']=$this->uri->segment(1);
		    $data['template']=$this->uri->segment(2);
		    $data['level_data'] = array('机密','秘密','内部');
		    $data['update_data'] = array('库存已更新','库存未更新','发放已更新','发放未更新');
		    $data['title']="资料统计-用户资料管理系统";
		    $this->load->view('top',$data);
		}
}
